﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Wicresoft.Common;
using Wicresoft.Framework.Organization;
using JDEduTrain.Framework.Business;
using JDEduTrain.Framework.ViewEntity;
using JDEduTrain.Framework.Entity;

using NameControlLib;

public partial class Page_MinistryAdminList : BaseAuthenticationPage
{

    public string SearchDefaultText
    {
        get
        {
            return "搜索 姓名";
        }
    }

    public string SearchClause
    {
        get
        {
            if (ViewState["SearchClause"] == null)
                ViewState["SearchClause"] = "1=1";
            return ViewState["SearchClause"].ToString();
        }
        set
        {
            ViewState["SearchClause"] = value;
        }
    }
    protected DataTable DTUserPageList
    {
        get
        {
            if (ViewState["DTUserPageList"] != null)
            {
                return (DataTable)ViewState["DTUserPageList"];
            }
            return new DataTable();
        }
        set
        {
            ViewState["DTUserPageList"] = value;
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        this.tbSearch.Attributes.Add("onfocus", string.Format("clearText(this, '{0}');", SearchDefaultText));
        this.tbSearch.Attributes.Add("onblur", string.Format("insertText(this, '{0}');", SearchDefaultText));
        this.tbSearch.Attributes.Add("onkeydown", "searchClick();");
        this.ShowLRLayoutButton = false;
        this.ShowUDLayoutButton = false;
        this.ShowSimpleLayoutButton = false;
        this.DefaultLayout = "Simple";
        this.ShowAdvancedSearch = true;

        this.Title = "基层管理员列表";
        if (!IsPostBack)
        {
            tbSearch.Text = SearchDefaultText;
            this.Order.OrderColumn = UserExtend.SQLCOL_THIRDLEVELDEPTDISPLAYNAME;
            this.Order.OrderDirection = "DESC";
            this.ShowAdvancedSearch = false;
            BindData();
        }

    }

    protected override void Pager_PageChanged(object sender, EventArgs e)
    {
        BindData();
    }

    protected override void Order_OrderChanged(object sender, EventArgs e)
    {
        BindData();
    }

    private void BindData()
    {
        int recordCount = 0;

        DataTable dtMinistryAdmin = OrganizationHelper.GetUserInfoTableByRole(AppConfig.MinistryAdminRoleID, true);
        recordCount = dtMinistryAdmin.Rows.Count;
        DTUserPageList = WebCommon.GetPagedTable(dtMinistryAdmin, Pager.PageIndex + 1, Pager.PageSize);
        Pager.RecordCount = recordCount;
        DTUserPageList.Columns.Add("ThirdLevelDeptDisplayName");
        foreach (DataRow dr in DTUserPageList.Rows)
        {
            dr["ThirdLevelDeptDisplayName"] = WebCommon.GetThirdLevelDepartmentName(new Guid(dr["MainDepartmentID"].ToString()));
        }
        DataView dv = DTUserPageList.DefaultView;
        dv.RowFilter = SearchClause;
        rptUserInfo.DataSource = DTUserPageList;
        rptUserInfo.DataBind();

    }

    protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindData();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (tbSearch.Text != string.Empty && tbSearch.Text != SearchDefaultText)
            SearchClause = string.Format("{0} LIKE '%{1}%'", "DisplayName", tbSearch.Text);
        else
            SearchClause = "1=1";
        BindData();
    }
    protected void btnAdvancedSearch_Click(object sender, EventArgs e)
    {
        if (tbSearchTitle.Text != string.Empty)
            SearchClause = string.Format("{0} LIKE N'%{1}%'", UserExtend.SQLCOL_FULLNAME, tbSearchTitle.Text);
        else
            SearchClause = "1=1";

        if (tbSearchDept.Text != string.Empty)
            SearchClause += string.Format("AND {0} LIKE N'%{1}%'", UserExtend.SQLCOL_THIRDLEVELDEPTDISPLAYNAME, tbSearchDept.Text);

        BindData();

    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindData();
    }


    protected void btnAddUser_Click(object sender, EventArgs e)
    {
        string[] userIDs = tbUserID.Text.Split(';');
        foreach (string userID in userIDs)
        {
            if (!string.IsNullOrEmpty(userID))
            {
                try
                {
                    OrganizationHelper.AddUserIntoRole(new Guid(userID), AppConfig.MinistryAdminRoleID);
                }
                catch (Exception)
                { }
            }
        }
        tbUserDisplayName.Text = "";
        tbUserID.Text = "";
        tbUserName.Text = "";
        BindData();
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        ArrayList alUser = new ArrayList();

        for (int i = 0; i < rptUserInfo.Items.Count; i++)
        {
            if ((rptUserInfo.Items[i].FindControl("cbUser") as CheckBox).Checked)
            {
                Guid id = new Guid(DTUserPageList.Rows[rptUserInfo.Items[i].ItemIndex]["ID"].ToString());
                try
                {
                    OrganizationHelper.RemoveUserFromRole(id, AppConfig.MinistryAdminRoleID);
                }
                catch (Exception)
                { }
            }
        }
        BindData();
    }
    protected void rptUserInfo_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Label lFullName = e.Item.FindControl("lFullName") as Label;

            string id = DTUserPageList.Rows[e.Item.ItemIndex]["ID"].ToString();
            NameControlLib.NameInfo userControl = new NameInfo();
            userControl.ID = "UserNameControl_" + id;
            userControl.UserID = new Guid(id);
            userControl.ScriptPath = "../Js/NameInfo.js";
            lFullName.Controls.Add(userControl);
            if (string.IsNullOrEmpty(lFullName.Text))
            {

            }

        }
    }

}
